﻿// -----------------------------------------------------------------------
// <copyright file="NoShortVariableNames.cs" company="AXA-IM">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace StyleCop.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using StyleCop.CSharp;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [SourceAnalyzer(typeof(CsParser))]
    public class NoShortVariableNames : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            CsDocument csdocument = (CsDocument)document;

            if (csdocument.RootElement != null && !csdocument.RootElement.Generated)
            {
                csdocument.WalkDocument(new CodeWalkerElementVisitor<object>(this.VisitElement), null, null);
            }
        }


        private bool VisitElement(CsElement element, CsElement parentElement, object context)
        {
            if (element.ElementType == ElementType.Field)
            {
                if (element.Declaration.Name.Length <= 3)
                    base.AddViolation(element, "NoShortVariableNames", new object[0]);
            }
            if (element.ElementType == ElementType.Method)
            {
                foreach (Variable elem in element.Variables)
                {
                    if (!elem.Type.Text.ToLower().EndsWith("EventArgs".ToLower()))
                    {
                        if (elem.Name.Length <= 3)
                            base.AddViolation(element, elem.Location.LineNumber, "NoShortVariableNames", new object[0]);
                    }
                }
            }
            return true;
        }
    }
}
